package com.xls.app;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;

import tv.danmaku.ijk.media.example.activities.VideoActivity;
import tv.danmaku.ijk.media.example.widget.media.AndroidMediaController;
import tv.danmaku.ijk.media.example.widget.media.IjkVideoView;

/**
 * Created by Administrator on 2017/9/15.
 */

public class VideoListAdpater extends RecyclerView.Adapter<VideoListAdpater.ViewHolder> {


    private List<VideoBean> videos;
    private Context context;

    public VideoListAdpater(List<VideoBean> videos, Context context) {
        this.videos = videos;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.video_list_item, viewGroup, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int i) {
        final VideoBean videoBean = videos.get(i);
        holder.tvTitle.setText(videoBean.getTitle());
        holder.flTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =  VideoActivity.newIntent(v.getContext(),videoBean.getUrl(),videoBean.getTitle());
                v.getContext().startActivity(intent);
               /* holder.videoView.start();
                holder.imageView.setVisibility(View.GONE);*/
            }
        });
        /*MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
        mediaMetadataRetriever.setDataSource(videoBean.getUrl(),new HashMap<String, String>());
        Bitmap bitmap = mediaMetadataRetriever.getFrameAtTime(1000);
        holder.imageView.setImageBitmap(bitmap);*/

       /* holder.videoView.setMediaController(new AndroidMediaController(context, false));
        holder.videoView.setVideoPath(videoBean.getUrl());*/


    }

    @Override
    public int getItemCount() {
        return videos.size();
    }

    public List<VideoBean> getVideos() {
        return videos;
    }

    public void setVideos(List<VideoBean> videos) {
        this.videos = videos;
        notifyDataSetChanged();
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvTitle;
        FrameLayout flTest;
        ImageView imageView;
        IjkVideoView videoView;
        public ViewHolder(View itemView) {
            super(itemView);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            flTest = (FrameLayout) itemView.findViewById(R.id.fl_test);
            imageView = (ImageView) itemView.findViewById(R.id.iv_img);
            videoView = (IjkVideoView) itemView.findViewById(R.id.video_view);
        }
    }
}
